var width = 330;
var long_pause = 10000;
var short_pause = 1;
var t;
var direction = -1;
var debug = true;

add_css ("/modules/news/css/news.css");



function basename (url) {
    var base_name=url.lastIndexOf("/")+1;
    return (url.substring(base_name));
}

function start_scroll_images () {
 //   setTimeout ( "scroll_images ()", long_pause );
}

/*
function getFile(url) {
  if (window.XMLHttpRequest) {              
    AJAX=new XMLHttpRequest();              
  } else {                                  
    AJAX=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (AJAX) {
     AJAX.open("GET", url, false);                             
     AJAX.send(null);
     return AJAX.responseText;                                         
  } else {
     return false;
  }                                             
}
*/

function scroll_images () {
	//var news_text_box = document.getElementById("news_text_box");
    var images = document.getElementsByTagName('IMG');
    var news_scroll_left = document.getElementById("news_scroll_left");
    
    
    var scroll_images = [];
    var s = 0;
    var rh = -1*width;
    for (var i=0; i<images.length; i++) {
        if (images[i].parentNode.id == "news_left_box") {
        	 //if (debug) console.log(images[i].parentNode.id);
             scroll_images[s]=images[i];
             s++;
             rh += width;
        }
    }

    interval = short_pause;
    
    for (var i=0; i<scroll_images.length; i++) {
            var l= parseInt(scroll_images[i].style.left) + direction;
            
            if ((direction==-1)&&(l < -1 * width)) l = rh;
            else if ((direction==1)&&(l > rh )) l = -1 * width;
            
            scroll_images[i].style.left = l + "px";
            
            if  (l==0) {
                
                //news_text_box.innerHTML = getFile(text_data[basename(scroll_images[i].src)]);
            	populate_text_box (basename(scroll_images[i].src));
                interval = long_pause;
                populate_scroll_box ("news_s3", scroll_images[i].src);
            } else if (l==330) {
                populate_scroll_box ("news_s4", scroll_images[i].src);
           /* } else if (l==660) {
                populate_scroll_box ("news_s5", scroll_images[i].src); */
            } else if (l==-330) {
                populate_scroll_box ("news_s2", scroll_images[i].src);
            }
    }
    //if (debug) console.log(news_scroll_left.innerHTML);
   
    t = setTimeout ( "scroll_images ()", interval );
}

function populate_text_box (img_file) {
	/*console.log(img_file);
	console.log(text_data[img_file]);
	console.log("content:" + getFile(text_data[img_file]));*/
	var content_file = text_data[img_file];
	var content = "false";
	while (content=="false") {
		//console.log(1);
		content = getFile(content_file);
	}
	//console.log(content);
	
	
	var news_text_box = document.getElementById("news_text_box");
	news_text_box.innerHTML =  content;
}

function populate_scroll_box (box, src) {
    
    var s =document.getElementById(box);
    s.innerHTML = "<img src=" + src + " />";
    
    //Fill in other boxes
}

function end () {
   // console.log ("Paused");
    return false;
} 

function scroll_left () {
    direction = -1;
    reset_timer ();
}

function scroll_right () {
    direction = 1;
    reset_timer ();
}

function reset_timer () {
    clearTimeout(t);
    t = setTimeout ( "scroll_images ()", short_pause );
}

